package org.qcit.plugin.tasks;




import com.android.build.gradle.AppExtension;
import com.android.build.gradle.api.ApplicationVariant;
import com.android.build.gradle.api.BaseVariant;
import com.google.gson.Gson;

import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.TaskAction;
import org.qcit.plugin.Extension;
import org.qcit.plugin.api.PgyerUploadInterface;
import org.qcit.plugin.api.RetrofitUtil;

import java.io.File;

import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 上传apk到蒲公英
 */
public class PgyerUploadTask extends DefaultTask {
    public BaseVariant variant;
    public Project project;

    public void init(ApplicationVariant variant, Project project) {
        this.project = project;
        this.variant = variant;
        setDescription("更新apk 到蒲公英");
        setGroup("打包上传");
    }

    @Override
    public Task doFirst(Action<? super Task> action) {
        System.out.println("===================doFirst========================：" );
        return super.doFirst(action);
    }

    @TaskAction
    public void upload() {
        System.out.println("===================upload========================：" );
        AppExtension appExtension = (AppExtension) project.getExtensions().findByName("android");
        variant.getOutputs().forEach(it -> {
            File apkFile = it.getOutputFile();
            if (apkFile == null || !apkFile.exists()) {
                throw new GradleException(apkFile + "不存在！");
            } else {
                System.out.println("文件打包完成准备上传：" + apkFile.getAbsoluteFile());
            }
            Extension extension = Extension.getConfig(project);
            System.out.println("#############################");
            System.out.println("#appicationId: " + variant.getMergedFlavor().getApplicationId());
            System.out.println("#uploadFileName: " + apkFile.getAbsoluteFile());
            System.out.println("#versionName: " + appExtension.getDefaultConfig().getVersionName());
            System.out.println("#versionCode: " + appExtension.getDefaultConfig().getVersionCode());
            System.out.println("#appName: " + extension.appName);
            System.out.println("#changeLoag: " + extension.changeLog);
            System.out.println("#############################");
            System.out.println("文件开始上传：" + apkFile.getAbsoluteFile());
            try {
                Response<ResponseBody> appResponse = RetrofitUtil.
                        getRetrofit("https://www.pgyer.com/apiv2/").
                        create(PgyerUploadInterface.class).
                        upload(RetrofitUtil.getTextBody(extension.pgyApiKey),
                                RetrofitUtil.getFilePart("application/vnd.android.package-archive", apkFile),
                                RetrofitUtil.getTextBody("2"),
                                RetrofitUtil.getTextBody("123456"),
                                RetrofitUtil.getTextBody("test"),
                                RetrofitUtil.getTextBody("xiangxue"),
                                RetrofitUtil.getTextBody("2")).execute();
                System.out.println("appResponse: " + appResponse.message());
                System.out.println("appResponse: " + new Gson().toJson(appResponse.body().toString()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
